package com.xy.p2p.action.front;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tfinanceinfo;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.service.TfinanceinfoService;
import com.xy.p2p.service.TmemberassetaccountService;
import com.xy.p2p.service.TmemberassetaccountdetailsService;
import com.xy.p2p.service.TtenderrecordService;



@Controller
public class TmemberassetaccountAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberassetaccountAction.class);
	public TmemberassetaccountAction() {
	}
	
	@RequestMapping("member/v_tmemberassetaccountList.jspx")
	public String list(HttpServletRequest request, ModelMap model, Integer pageNo) {
		Tmemberassetaccount tmemberassetaccount = new Tmemberassetaccount();
		tmemberassetaccount.setTmember((Tmember) request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		List<Tmemberassetaccount> list = tmemberassetaccountService.getTmemberassetaccount(toMap(tmemberassetaccount));
		if (list.size() <= 0)
			list.add(tmemberassetaccount);
		model.addAttribute("entity", list.get(0));
		setDict(model);
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberassetaccountService.getTmemberassetaccountByPage(toMap(tmemberassetaccount), pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		// 查询余额贷的收入记录
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Tmember tmember = (Tmember) request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		queryMap.put("Tmember", tmember);
		List<Ttenderrecord> ttlist = ttenderrecordService.getTtenderrecord(queryMap);
		double yedtotal = 0;
		// 普通投标数
		double nCount = 0;
		// 普通投标利率和
		double npa = 0;
		// 余额贷投标数
		double yednCount = 0;
		// 余额贷投标利率和
		double yednpa = 0;
		for (Ttenderrecord t : ttlist) {
			if (8 == t.getTfinanceinfo().getTargettype()) {
				yedtotal += t.getMibmoney();
				yednCount++;
				yednpa += t.getFinancepa();
			} else {
				nCount++;
				npa += t.getFinancepa();
			}
		}
		List<Tmemberassetaccountdetails> tt = tmemberassetaccountdetailsService.getTmemberassetaccountdetails(queryMap);
		double yedincome = 0;
		for (Tmemberassetaccountdetails t : tt) {
			if (1 == t.getBorrowing() && 28 == t.getTrantype()) {
				yedincome += t.getTranamount();
			}
		}
		model.addAttribute("yedtotal", yedtotal);
		model.addAttribute("yedincome", yedincome);
		// 增加查询待交充值管理费
		model.addAttribute("rechrageFee", tmemberassetaccountdetailsService.getUnPayRechargeFee(tmember));
		// 获取投资总额
		model.addAttribute("asset", tmemberassetaccountService.getTotalAsset(tmember));
		model.addAttribute("borrow", tmemberassetaccountService.getTotalBorrow(tmember));
		// 查询投标总收入
		model.addAttribute("income", tmemberassetaccountService.getTotalIncome(tmember));
		model.addAttribute("nCount", nCount);
		if (npa == 0 || nCount == 0)
			model.addAttribute("npa", 0);
		else
			model.addAttribute("npa", npa / nCount);
		// 余额贷平均利率和投标总数
		model.addAttribute("yednCount", yednCount);
		if (yednpa == 0 || yednCount == 0)
			model.addAttribute("yednpa", 0);
		else
			model.addAttribute("yednpa", yednpa / yednCount);
		// 融资列表
		queryMap.put("status", 7);
		List<Tfinanceinfo> tempList = new ArrayList<Tfinanceinfo>();
		for (Tfinanceinfo t : tfinanceinfoService.getTfinanceinfo(queryMap)) {
			if (t.getTargettype() != 8) {
				tempList.add(t);
			}
		}
		model.addAttribute("financeList", tempList);
		// 用于导航栏
		model.addAttribute("dis", 1);
		return "/front/member/accountInfo.html";
	}
 
	@RequestMapping("/member/v_tmemberassetaccountToWidrapp.jspx")
	public String ToWidrapp(HttpServletRequest request, ModelMap model) {
		Tmemberassetaccount tmemberassetaccount = new Tmemberassetaccount();
		Tmember tmember = (Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		tmemberassetaccount.setTmember(tmember);
		List<Tmemberassetaccount> list = tmemberassetaccountService.getTmemberassetaccount(toMap(tmemberassetaccount));
		if(list.size()<=0)
			list.add(tmemberassetaccount);
		model.addAttribute("entity", list.get(0));
        setDict(model);
      //增加查询待交充值管理费
      		model.addAttribute("rechrageFee", tmemberassetaccountdetailsService.getUnPayRechargeFee(tmember));
        //用于导航栏
        model.addAttribute("dis", 7);
		return "/front/member/twidrapp.html";
	}
	
	
	/**
	 * 会员资产账户信息表查询
	 * 
	 * @param tmemberassetaccount
	 * @param request
	 * @param model
	 * @return
	 */
//	@RequestMapping("v_tmemberassetaccountList1.do")
//	public String list(Tmemberassetaccount tmemberassetaccount, HttpServletRequest request, ModelMap model) {
//		List<Tmemberassetaccount> list = tmemberassetaccountService.getTmemberassetaccount(toMap(tmemberassetaccount));
//		model.addAttribute("list", list);
//        setDict(model);
//		return "tmemberassetaccountList";
//	}
	//测试电子账单
	@RequestMapping("v_tmemberassetaccountbilling.jspx")
	public String billing(){
		tmemberassetaccountService.billing(68);
		return "test.html";
	}

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmemberassetaccount
	 * @return
	 */	private Map<String,Object> toMap(Tmemberassetaccount tmemberassetaccount){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmemberassetaccount.getMaaid()!=null&&!tmemberassetaccount.getMaaid().equals("")){
			map.put("maaid", tmemberassetaccount.getMaaid());
		}
		if(tmemberassetaccount.getTmember()!=null){
			map.put("Tmember", tmemberassetaccount.getTmember());
		}
		if(tmemberassetaccount.getAmountfrozen()!=null&&!tmemberassetaccount.getAmountfrozen().equals("")){
			map.put("amountfrozen", tmemberassetaccount.getAmountfrozen());
		}
		if(tmemberassetaccount.getAfreason()!=null&&!tmemberassetaccount.getAfreason().equals("")){
			map.put("afreason", tmemberassetaccount.getAfreason());
		}
		if(tmemberassetaccount.getBalance()!=null&&!tmemberassetaccount.getBalance().equals("")){
			map.put("balance", tmemberassetaccount.getBalance());
		}
		if(tmemberassetaccount.getStatus()!=null&&!tmemberassetaccount.getStatus().equals("")){
			map.put("status", tmemberassetaccount.getStatus());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TMEMBERASSETACCOUNT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmemberassetaccount tmemberassetaccount, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmemberassetaccountService tmemberassetaccountService;  
	@Resource
	private TmemberassetaccountdetailsService tmemberassetaccountdetailsService; 
	@Resource
	private TtenderrecordService ttenderrecordService;
	@Resource
	private TfinanceinfoService tfinanceinfoService; 
}